var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var app = getApp();
Page({
  data: {
    district1:[],
    arr_district1:[],
    index1:0,

    district2:[],
    arr_district2:[],
    index2:0,

    district3:[],
    arr_district3:[],
    index3:0,

    info:{
      TrueName:'',
      ProvinceID:0,
      CityID:0,
      PartID:0,
      CompanyName:'',
      Service:'',
      Job:'',
      Tel:'',
      Code:'',
      CarNum:'',
      Address:''
    }
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成
    var userInfo=wx.getStorageSync('userInfo');
    if(userInfo!="")
    {
      this.setData({info:userInfo});
      this.get_district();
    }
    

  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  get_district:function()
  {
    var that=this;
    var info = this.data.info;
    
    


    util.request(api.GetDistrictList,{Pid:1},'GET').then(function (res) {
      if (res.Status== 100) 
      {
        that.setData({district1:res.Data});
        var arr=new Array();
        res.Data.map(function(item,i){
          arr.push(item.Name);
          if(info.ProvinceID==item.ID)
          {
             that.setData({index1:i});
          }
        });
        that.setData({arr_district1:arr});
        
        
        
        var pid=info.ProvinceID;
        util.request(api.GetDistrictList,{Pid:pid},'GET').then(function (res1) {
          if (res1.Status== 100) 
          {
            that.setData({district2:res1.Data});
            var arr=new Array();
            res1.Data.map(function(item,ii){
              arr.push(item.Name);
              if(info.CityID==item.ID)
              {
                 that.setData({index2:ii});
              }
            });
            that.setData({arr_district2:arr}); 
            
            var cid=info.CityID;
            util.request(api.GetDistrictList,{Pid:cid},'GET').then(function (res3) {
              if (res3.Status== 100) 
              {
                that.setData({district3:res3.Data});
                var arr=new Array();
                res3.Data.map(function(item,k){
                  arr.push(item.Name);
                  if(info.PartID==item.ID)
                  {
                    that.setData({index3:k});
                  }
                });
                that.setData({arr_district3:arr});
        
              }
            });
            
    
          }
        });

      }
    });

    
  },
  bindPickerChange1: function (e) 
  {
    var that = this;
    var index = e.detail.value;
    this.setData({ index1: index });
    var district1 = that.data.district1;
    var info = that.data.info;
    info.ProvinceID = district1[index].ID;
    that.setData({ info: info });

    util.request(api.GetDistrictList,{Pid:info.ProvinceID},'GET').then(function (res) {
      if (res.Status== 100) 
      {
        that.setData({district2:res.Data});
        var arr=new Array();
        res.Data.map(function(item){
          arr.push(item.Name);
        });
        that.setData({arr_district2:arr});
      }
    });
  },
  bindPickerChange2: function (e) 
  {
    var that = this;
    var index = e.detail.value;
    this.setData({ index2: index });
    var district2 = that.data.district2;
    var info = that.data.info;
    info.CityID = district2[index].ID;
    that.setData({ info: info });

    util.request(api.GetDistrictList,{Pid:info.CityID},'GET').then(function (res) {
      if (res.Status== 100) 
      {
        that.setData({district3:res.Data});
        var arr=new Array();
        res.Data.map(function(item){
          arr.push(item.Name);
        });
        that.setData({arr_district3:arr});
      }
    });
  },
  startRegister: function () 
  {
    var that = this;
    var info = that.data.info;
    if(info.TrueName==''||info.Job==''||info.Tel==''||info.Address=='')
    {
      wx.showModal({title:'提示',content:'请输入姓名、职业、电话、收货地址'});
      return;
    }
    var userInfo=wx.getStorageSync('userInfo');
    info.Id=userInfo.Id;
    info.Mobile=userInfo.Mobile;
    info.UserName=userInfo.UserName;
    info.ST=1;
    info.RoleID=userInfo.RoleID;
    info.CreateDate=userInfo.CreateDate;
    info.ShopID=userInfo.ShopID;
    info.FromID=userInfo.FromID;
    info.OpenID=userInfo.OpenID;
    info.UserIcon=userInfo.UserIcon;
    util.request(api.UsersUpdateInfo,info,'POST').then(function (res) {
      if (res.Status== 100) 
      {
        wx.showModal({
          title: '提示',
          content:'信息提交成功',
          success(res1) 
          {
            wx.reLaunch({url: '/pages/index/index'})
          }
        })

         
      }
      else
      {
        wx.showModal({title:'提示',content:'信息提交操作错误'});
        return;
      }
    });

     
  },
  bindTrueNameInput: function (e) 
  {
    var info = this.data.info;
    info.TrueName = e.detail.value;
    this.setData({ info: info });
  },
  bindCompanyNameInput: function (e) 
  {
    var info = this.data.info;
    info.CompanyName = e.detail.value;
    this.setData({ info: info });
  },
  bindServiceInput: function (e) 
  {
    var info = this.data.info;
    info.Service = e.detail.value;
    this.setData({ info: info });
  },
  bindJobInput: function (e) 
  {
    var info = this.data.info;
    info.Job = e.detail.value;
    this.setData({ info: info });
  },
  bindTelInput: function (e) 
  {
    var info = this.data.info;
    info.Tel = e.detail.value;
    this.setData({ info: info });
  },
  bindCodeInput: function (e) 
  {
    var info = this.data.info;
    info.Code = e.detail.value;
    this.setData({ info: info });
  },
  bindCarNumInput: function (e) 
  {
    var info = this.data.info;
    info.CarNum = e.detail.value;
    this.setData({ info: info });
  },
  bindAddressInput: function (e) 
  {
    var info = this.data.info;
    info.Address = e.detail.value;
    this.setData({ info: info });
  },

  clearInput: function (e) 
  {
    var info = this.data.info;
    switch (e.currentTarget.id) 
    {
      case 'clear-TrueName':
        info.TrueName="";
        break;

      case 'clear-CompanyName':
        info.CompanyName="";
        break;
        case 'clear-Service':
        info.Service="";
        break;
        case 'clear-Job':
        info.Job="";
        break;
        case 'clear-Tel':
        info.Tel="";
        break;
        case 'clear-Code':
        info.Code="";
        break;
        case 'clear-CarNum':
        info.CarNum="";
        break;
        case 'clear-Address':
        info.Address="";
        break;
       
    }
    this.setData({ info: info });
  }
})